vimfandomcom-20200223-history
Map caps lock to escape in Windows
This tip shows some methods for mapping keys within the operating system to make life easier in Vim. In particular, it can be convenient to use the CapsLock key for Escape so you don't have to reach for the Esc key. It is also useful to use the CapsLock key for Ctrl and press Ctrl-[ instead of Esc. This tip is for Windows systems (not Windows 9x). For Unix-based systems, see Map caps lock to escape in XWindows. Some of the procedures here change the way keys behave for all applications. It may be better to use other methods to avoid the escape key in Vim. Registry Before trying the following, you should run regedit.exe and inspect the relevant registry keys, and export them for a backup. Make sure you know how to remove the registry changes if they cause trouble. For the current user: To map CapsLock to Escape, and ScrollLock to CapsLock, create file (for example) keys.reg containing: REGEDIT4 Layout "Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,46,00,01,00,3a,00,00,00,00,00 Use Explorer to double-click the file to import the value to the registry. To apply the changes, log off and log on. Then, pressing key Esc will generate Escape as normal, pressing CapsLock will also generate Escape, and pressing ScrollLock will generate CapsLock (and you cannot generate ScrollLock). Alternatively, for the change to apply to all users, run the following reg file and reboot: REGEDIT4 Layout "Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,46,00,01,00,3a,00,00,00,00,00 To restore the keyboard, run regedit.exe and delete the "Scancode Map" entry from HKEY_CURRENT_USER (and log off/on), or from HKEY_LOCAL_MACHINE (and reboot), depending on which section of the registry you changed. Use the Caps Lock key as Ctrl Particularly while touch typing, it can be useful to remap the keyboard so that pressing CapsLock is the same as pressing Ctrl. This change makes it easy to press Ctrl-[ as an alternative to pressing Esc. The following applies to the current user (log off and log on to apply). Alternatively, an administrator can apply the change to all users (reboot to apply) by replacing Layout with Layout Map CapsLock to LeftCtrl (press CapsLock to generate LeftCtrl): REGEDIT4 Layout "Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1d,00,3a,00,00,00,00,00 An alternative would be to replace the "Scancode Map" line with the following to swap CapsLock and LeftCtrl: "Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,1d,00,1d,00,3a,00,00,00,00,00 Utilities As an alternative to changing the registry, there are various utilities that can remap keys. Many of these can be run by a non-administrator, and can apply mappings only when wanted (for example, when running Vim). They do not require log off/on or reboot to apply changes. KeyTweak This tool is a free keyboard remapper for Windows NT/2000/XP/Vista/Win 7. It automates the registry trick above (it makes use of Microsoft's Scancode Map registry key) to remap your keyboard. http://webpages.charter.net/krumsick/ AutoHotkey AutoHotkey is an open source project to automate sending keys and mouse clicks. It can remap keys, and provides a powerful scripting language, and does not require administrator rights. To make Capslock work like Escape, add the following to the default script: Capslock::Esc The following AutoHotkey script will turn off CapsLock and generate Escape when Esc is pressed: Esc:: SetCapsLockState, off Suspend On Send, {ESC} Suspend Off The following AutoHotkey script maps CapsLock to Ctrl in PuTTY. In other applications, CapsLock works normally. classname = "" keystate = "" *CapsLock:: WinGetClass, classname, A if (classname = "PuTTY") send,{Ctrl down} else GetKeyState, keystate, CapsLock, T if (keystate = "D") SetCapsLockState, Off else SetCapsLockState, On return *CapsLock up:: WinGetClass, classname, A if (classname = "PuTTY") send,{Ctrl up} return HotKeyz HotKeyz (freeware) is useful for assigning keyboard shortcuts to run frequently used applications using the key. It also allows CapsLock to be remapped to any key sequence, or to run an application. The only disadvantage compared to the registry method is that the status of CapsLock is still set and is checked by applications, so if you press it letting the status LED of the keyboard turn on and then start an application, for example Notepad, you will start typing in uppercase and can't change it anymore. You have then to exit the application, turn the CapsLock status off and re-open the program. This is still easier than rebooting. Explanations Registry scancodes The format used for "Scancode Map" in the registry is described below (from Scan Code Mapper for Windows and Keyboard scan codes). More information is at Remap the F12 Key, CapsLock Key or any Key in Windows XP, 2000, Vista and 2003. "Scancode Map" is a registry binary value with the following format (each value is a 32-bit integer in little-endian order): <00000000> <00000000> ... <00000000> A mapping consists of two 16-bit scancodes. The second scancode (high word) from the keyboard is replaced with the first scancode (low word). Some relevant scancodes are: 0x01 Esc 0x1D L-Ctrl 0x38 L-Alt 0x3A CapsLock 0x46 ScrollLock 0x0E Backspace For example, the line "Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,01,00,3a,00,00,00,00,00 consists of one mapping (01,00,3a,00) which replaces the scancode 0x003a with 0x0001. That means that pressing CapsLock (0x3a) will generate Escape (0x01). See also *Discussion page for more details and alternatives *Avoid the escape key for Vim methods to make pressing Esc easier Comments